package com.gljx.web.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.GetFid;
import com.gljx.web.Entity.OtherCarAddOil;
import com.gljx.web.Entity.OtherCarAddOilEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.OtherCarAddOilEntryMapper;
import com.gljx.web.dao.OtherCarAddOilMapper;
import com.gljx.web.service.OtherCarAddOilService;

@Service
@Transactional
public class OtherCarAddOilServiceImpl extends CommonDao implements OtherCarAddOilService{

	@Autowired
	private OtherCarAddOilEntryMapper otherCarAddOilEntryMapper;
	
	@Autowired
	private OtherCarAddOilMapper otherCarAddOilMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.OtherCarAddOilMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.OtherCarAddOilMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<OtherCarAddOilEntry> queryOtherCarAddOilEntryMapper(String fparentId, String dept, String car) {
		// TODO Auto-generated method stub
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("v_fparentid", fparentId);
	       paramMap.put("v_cfdept", dept);
	       paramMap.put("v_cfcar", car);
	       paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.OtherCarAddOilEntryMapper.selectOtherCarAddOilEntryMapper", paramMap, null);
	       @SuppressWarnings("unchecked")
	       List<OtherCarAddOilEntry> otherCarAddOilEntrys = (List<OtherCarAddOilEntry>) paramMap.get("v_cursor");
	       return otherCarAddOilEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editOtherCarAddOil(OtherCarAddOil otherCarAddOil, String[] fseq, String[] cfcar, String[] cfdept,
			String[] cfoilspecification, String[] cfqty, String[] cfremarks, User user, String time) {
		// TODO Auto-generated method stub
		if("".equals(otherCarAddOil.getFid()) || otherCarAddOil.getFid() == null){
			//创建人id
			otherCarAddOil.setFcreatorid(user.getFid());
			//利用UUID生成fid
			otherCarAddOil.setFid(GetFid.UUIDIntoFid());
			//当前时间
			otherCarAddOil.setFcreatetime(time);
			//状态
            otherCarAddOil.setCfbillstate(3L);
            //添加
			otherCarAddOilMapper.insertSelective(otherCarAddOil);
		}else{
			//修改时间时间
			otherCarAddOil.setFlastupdatetime(time);
			//修改人id
			otherCarAddOil.setFlastupdateuserid(user.getFid());
			//修改
			otherCarAddOilMapper.updateByPrimaryKeySelective(otherCarAddOil);
			//先删除子类信息
			otherCarAddOilEntryMapper.deleteByFparentId(otherCarAddOil.getFid());
		}
		//添加子类信息
		List<OtherCarAddOilEntry> otherCarAddOilEntrys = new ArrayList<OtherCarAddOilEntry>();
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				OtherCarAddOilEntry otherCarAddOilEntry = new OtherCarAddOilEntry();
				otherCarAddOilEntry.setCfcar(cfcar[i]);
				otherCarAddOilEntry.setCfdept(cfdept[i]);
				otherCarAddOilEntry.setCfoilspecification(cfoilspecification[i]);
				if("".equals(cfqty[i])){
					
				}else{
					otherCarAddOilEntry.setCfqty(new BigDecimal(cfqty[i]));
				}
				otherCarAddOilEntry.setCfremarks(cfremarks[i]);
				otherCarAddOilEntry.setFseq((long)i+1);
				otherCarAddOilEntry.setFid(GetFid.UUIDIntoFid());
				otherCarAddOilEntry.setFparentid(otherCarAddOil.getFid());
				otherCarAddOilEntrys.add(otherCarAddOilEntry);
			}
			otherCarAddOilEntryMapper.insertOtherCarAddOilEntry(otherCarAddOilEntrys);
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public OtherCarAddOil queryOtherCarAddOilByFid(String fid) {
		// TODO Auto-generated method stub
		return otherCarAddOilMapper.selectOtherCarAddOilByFid(fid);
	}

	/**
	 * 审核，反审核
	 */
	@Override
	public int auditOtherCarAddOil(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
		// TODO Auto-generated method stub
		OtherCarAddOil otherCarAddOil = new OtherCarAddOil();
		//fid
		otherCarAddOil.setFid(fid);
		//审核人
		otherCarAddOil.setFauditorid(fhandlerId);
		//修改人
		otherCarAddOil.setFlastupdateuserid(fhandlerId);
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
			if (serverResponse.getStatus() == 1) {
				otherCarAddOil.setCfbillstate(5L);
				//结束
				return otherCarAddOilMapper.updateByPrimaryKeySelective(otherCarAddOil);
			} else if (serverResponse.getStatus() == 2) {
                if (!"1".equals(state)) {
                    otherCarAddOil.setCfbillstate(3L);
                    otherCarAddOilMapper.updateByPrimaryKeySelective(otherCarAddOil);
                }
                //推进
				return 2;
			} else {
				//错误
				return 0;
			}
		}else{
            axamine.againstAuditProcess(fnumber, message);
            otherCarAddOil.setCfbillstate(3L);
			return otherCarAddOilMapper.updateByPrimaryKeySelective(otherCarAddOil);
		}
	}

	/**
	 * 根据fid
	 */
	@Override
    public void deleteOtherCarAddOilByFid(String fid, String[] deleteId, String[] fnumber, String fnumber2) {
        // TODO Auto-generated method stub
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        if(fid == null || "".equals(fid)){
            for (int i = 0; i < deleteId.length; i++) {
                //先删除子类信息
                otherCarAddOilEntryMapper.deleteByFparentId(deleteId[i]);
                //在删除父类信息
                otherCarAddOilMapper.deleteByPrimaryKey(deleteId[i]);
                //删除流程
                actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            }
        }else{
			//先删除子类信息
			otherCarAddOilEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			otherCarAddOilMapper.deleteByPrimaryKey(fid);
            //删除流程
            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber2);
        }
	}

    @Override
    public int submitOtherCarAddOil(OtherCarAddOil otherCarAddOil) {
        return otherCarAddOilMapper.updateByPrimaryKeySelective(otherCarAddOil);
    }

    @Override
    public List<OtherCarAddOilEntry> selectOtherCarAddOilByInfo(String fcompanyid, String addoilName, String cfdept, String cfcar, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("fcompanyid", fcompanyid);
        map.put("addoilName", addoilName);
        map.put("cfdept", cfdept);
        map.put("cfcar", cfcar);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        return otherCarAddOilEntryMapper.selectOtherCarAddOilByInfo(map);
    }

}
